/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.zhpan.viewpagerindicator.provider;

import ohos.agp.colors.RgbColor;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.PageSliderProvider;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;

import java.util.ArrayList;
import java.util.List;

/**
 * 适配器
 *
 * @since 2021-04-13
 */
public class PagerProvider extends PageSliderProvider {
    private List<String> colors = new ArrayList<>();

    /**
     * 默认构造器
     */
    public PagerProvider() {
        colors.add("#CCFF99");
        colors.add("#41F1E5");
        colors.add("#8D41F1");
        colors.add("#FF99CC");
    }

    @Override
    public int getCount() {
        return colors.size();
    }

    @Override
    public Object createPageInContainer(ComponentContainer componentContainer, int i) {
        Text text = new Text(componentContainer.getContext());
        ShapeElement shapeElement = new ShapeElement();
        shapeElement.setRgbColor(RgbColor.fromArgbInt(Color.getIntColor(colors.get(i))));
        ComponentContainer.LayoutConfig layoutConfig = new ComponentContainer.LayoutConfig(
            ComponentContainer.LayoutConfig.MATCH_PARENT, ComponentContainer.LayoutConfig.MATCH_PARENT);
        text.setLayoutConfig(layoutConfig);
        text.setBackground(shapeElement);
        componentContainer.addComponent(text);
        return text;
    }

    @Override
    public void destroyPageFromContainer(ComponentContainer componentContainer, int i, Object o) {
        componentContainer.removeComponent((Component) o);
    }

    @Override
    public boolean isPageMatchToObject(Component component, Object object) {
        return component == object;
    }
}
